<template>
  <div class="chat-sidebar">
    <div class="new-chat-btn">
      <el-button type="primary" plain icon="el-icon-plus" @click="newChat">开启新对话</el-button>
    </div>
    
    <div v-if="chatsList.length > 0" class="history-section">
      <div class="history-label">
        <span>历史对话</span>
        <el-button 
          type="text" 
          size="mini" 
          class="clear-all-btn"
          @click="confirmClearAll"
        >
          清空全部
        </el-button>
      </div>
      
      <div class="chat-history">
        <div 
          v-for="chat in chatsList" 
          :key="chat.id"
          class="chat-item"
          :class="{ 'active': currentChatId === chat.id }"
        >
          <div class="chat-item-content" @click="selectChat(chat.id)">
            <div class="chat-title">{{ chat.title }}</div>
          </div>
          <div class="chat-item-actions">
            <el-button
              type="text"
              icon="el-icon-delete"
              size="mini"
              @click.stop="confirmDelete(chat.id, chat.title)"
              class="delete-btn"
            ></el-button>
          </div>
        </div>
      </div>
    </div>
    
    <div v-else class="empty-history">
      <p>还没有聊天记录</p>
      <p>点击"开启新对话"开始聊天</p>
    </div>
  </div>
</template>

<script>
import { computed } from 'vue';
import { useStore } from 'vuex';
import { ElMessageBox } from 'element-plus';

export default {
  name: 'ChatSidebar',
  
  props: {
    currentChatId: {
      type: String,
      default: ''
    }
  },
  
  setup(props, { emit }) {
    const store = useStore();
    
    // 从Vuex获取聊天历史
    const chatsList = computed(() => store.getters.chatHistory);
    
    // 选择聊天
    const selectChat = (chatId) => {
      emit('select-chat', chatId);
    };
    
    // 新建聊天
    const newChat = () => {
      emit('new-chat');
    };
    
    // 确认删除单个聊天
    const confirmDelete = (chatId, title) => {
      ElMessageBox.confirm(
        `确定要删除"${title}"吗？此操作不可恢复。`,
        '删除确认',
        {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }
      ).then(() => {
        store.dispatch('deleteChat', chatId);
        // 如果删除的是当前选中的聊天，通知父组件
        if (chatId === props.currentChatId) {
          emit('select-chat', '');
        }
      }).catch(() => {
        // 用户取消删除，无需操作
      });
    };
    
    // 确认清空所有聊天
    const confirmClearAll = () => {
      if (chatsList.value.length === 0) return;
      
      ElMessageBox.confirm(
        '确定要清空所有聊天记录吗？此操作不可恢复。',
        '清空确认',
        {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }
      ).then(() => {
        store.commit('clearChats');
        emit('select-chat', '');
      }).catch(() => {
        // 用户取消清空，无需操作
      });
    };
    
    return {
      chatsList,
      selectChat,
      newChat,
      confirmDelete,
      confirmClearAll
    };
  }
}
</script>

<style scoped>
.chat-sidebar {
  height: 100%;
  padding: 16px 0;
  display: flex;
  flex-direction: column;
  border-right: 1px solid #e0e0e0;
  background-color: #f9f9f9;
}

.new-chat-btn {
  padding: 0 16px 16px;
}

.history-section {
  display: flex;
  flex-direction: column;
  flex: 1;
}

.history-label {
  padding: 8px 16px;
  font-size: 12px;
  color: #606266;
  border-bottom: 1px solid #ebeef5;
  margin-bottom: 8px;
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.clear-all-btn {
  color: #909399;
  padding: 0;
}

.clear-all-btn:hover {
  color: #f56c6c;
}

.chat-history {
  flex: 1;
  overflow-y: auto;
}

.chat-item {
  padding: 8px 12px;
  cursor: pointer;
  border-radius: 4px;
  margin: 0 6px 4px;
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.chat-item:hover {
  background-color: #f0f2f5;
}

.chat-item.active {
  background-color: #e6f1fc;
}

.chat-item-content {
  flex: 1;
  overflow: hidden;
  padding: 2px 4px;
}

.chat-title {
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  font-size: 14px;
}

.chat-item-actions {
  opacity: 0;
  transition: opacity 0.2s;
}

.chat-item:hover .chat-item-actions {
  opacity: 1;
}

.delete-btn {
  color: #909399;
  padding: 2px;
}

.delete-btn:hover {
  color: #f56c6c;
}

.empty-history {
  flex: 1;
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  color: #909399;
  font-size: 14px;
  text-align: center;
}

.empty-history p {
  margin: 6px 0;
}
</style> 